package com.life.notes.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.life.notes.dao.SysRoleMenuMapper;
import com.life.notes.dto.RoleMenuQueryPara;
import com.life.notes.model.SysRoleMenuEntity;
import com.life.notes.service.SysRoleMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 角色与权限业务实现
 * @Author Sans
 * @CreateTime 2019/9/14 15:57
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenuEntity> implements SysRoleMenuService {

    @Override
    public IPage<SysRoleMenuEntity> listPage(RoleMenuQueryPara filter) {
        Page<SysRoleMenuEntity> page = new Page<>(filter.getPage(),filter.getLimit());
        return this.baseMapper.selectRoleMenus(page, filter);
    }

    @Override
    public List<SysRoleMenuEntity> list(RoleMenuQueryPara filter) {
        return this.baseMapper.selectRoleMenus(filter);
    }

    @Override
    public void saveRoleMenu(RoleMenuQueryPara para) {
        Integer roleId = para.getRoleId();
        String menuIds = para.getMenuIds();
        this.baseMapper.deleteByRoleId( roleId );
        if(StringUtils.isNotBlank( menuIds )){
            String[] menuIdArrays = menuIds.split( "," );
            if(menuIdArrays.length > 0){
                for (String menuId : menuIdArrays) {
                    SysRoleMenuEntity roleMenu = new SysRoleMenuEntity();
                    roleMenu.setRoleId( roleId );
                    roleMenu.setMenuId( Integer.parseInt( menuId ) );
                    this.baseMapper.insert( roleMenu );
                }
            }
        }
    }

    @Override
    public Integer saveRoleMenu(SysRoleMenuEntity para) {
        if (para.getId()!=null) {
            this.baseMapper.updateById(para);
        } else {
            this.baseMapper.insert(para);
        }
        return para.getId();
    }
}